package com.hdax.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hdax.gulimall.product.entity.PmsAttrAttrgroupRelationEntity;
import com.hdax.gulimall.product.entity.PmsAttrEntity;
import com.hdax.gulimall.product.service.PmsAttrAttrgroupRelationService;
import com.hdax.gulimall.product.service.PmsAttrService;
import com.hdax.gulimall.product.service.PmsCategoryService;
import com.hdax.gulimall.product.vo.AttrGroupRelationVo;
import com.hdax.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.hdax.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hdax.gulimall.product.entity.PmsAttrGroupEntity;
import com.hdax.gulimall.product.service.PmsAttrGroupService;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.R;



/**
 * 属性分组
 *
 * @author xiaohou
 * @email s2687781461@qq.com
 * @date 2023-04-07 17:41:13
 */
@RestController
@RequestMapping("product/attrgroup")
public class PmsAttrGroupController {
    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;

    @Autowired
    private PmsCategoryService pmsCategoryService;

    @Autowired
    private PmsAttrService attrService;



    /**
     * 根据新增商品的分类id获取分组   以及分组下的所有属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId")Long catelogId){
        //查询当前分类的分组分类下的所有分组
       List<AttrGroupWithAttrsVo> list= pmsAttrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data",list);
    }




    @Autowired
    private PmsAttrAttrgroupRelationService pmsAttrAttrgroupRelationService;
    //http://localhost:88/api/product/attrgroup/attr/relation新增关联
    @RequestMapping("/attr/relation")
    public R andRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVo){
        pmsAttrAttrgroupRelationService.addRelaltion(attrGroupRelationVo);
        return R.ok();
    }

    //http://localhost:88/api/product/attrgroup/1/noattr/relation?t=1681781586176&page=1&limit=10&key=
    /**
     * 查询分组没有关联的所有属性
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String,Object> map){
        PageUtils entities=attrService.getNoAttr(attrgroupId,map);
        return R.ok().put("page",entities);
    }

    /**
     * 根据组的id 来删除组关联的属性
     * @param attrGroupRelationVo
     *
     *
     *     private Integer attrId;   组的id
     *     private Integer attrGroupId;     组关联的属性id
     *
     * @return
     */
    @RequestMapping("/attr/relation/delete")
    public R delete(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrService.deleteRelation(attrGroupRelationVo);
        return R.ok();

    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catlogId}")
    //@RequiresPermissions("product:pmsattrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catlogId){
        PageUtils pageUtils = pmsAttrGroupService.queryPage(params, catlogId);
        return R.ok().put("page", pageUtils);
    }

    ///product/attrgroup/1/attr/relation?t=1681720158176

    /**
     * 根据分组id来查询分组的属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<PmsAttrEntity> list=attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:pmsattrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);
        //组的id                         255
        Long getCatelogId = pmsAttrGroup.getCatelogId();
        Long[] attrgroups= pmsCategoryService.findCatelogPath(getCatelogId);
        pmsAttrGroup.setCatelogIds(attrgroups);
        return R.ok().put("pmsAttrGroup", pmsAttrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:pmsattrgroup:save")
    public R save(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.save(pmsAttrGroup);


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:pmsattrgroup:update")
    public R update(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.updateById(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:pmsattrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		pmsAttrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
